package com.dtu.distributed.thread;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.naming.AuthenticationNotSupportedException;

import com.dtu.distributed.controller.BullyAlgorithmControlller;
import com.dtu.distributed.controller.TempController;
import com.dtu.distributed.enums.Action;

public class AdminGetAnswerThread extends Thread{
	
	
	private Socket clientSocket;
	
	private DataOutputStream out;
	private DataInputStream in;
	
	public AdminGetAnswerThread( Socket clientSocket){
		this.clientSocket=clientSocket;
		try {
			out = new DataOutputStream(clientSocket.getOutputStream());
			in = new DataInputStream(clientSocket.getInputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		try {
			String messageOut = Action.ELECTION.toString() + ":" + BullyAlgorithmControlller.ownIdentifier;
			System.out.println("Sending out " + messageOut);

			out.writeUTF(messageOut);//send election

			String messageIn = in.readUTF();//recieve anwser

			if (Action.ANSWER == Action.getAction(messageIn)) {
				
				BullyAlgorithmControlller.answerRecieved = true;
				System.out.println("Answer recived.");
				
			}
			clientSocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
